package com.dgut.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.admin.param.ItemParam;
import com.dgut.admin.service.TypeService;
import com.dgut.common.entity.Category;
import com.dgut.common.entity.Item;
import com.dgut.common.mapper.CategoryMapper;
import com.dgut.common.mapper.ItemMapper;
import com.dgut.common.result.TypeResult;
import com.dgut.common.utils.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zyq
 * @Date 2022/4/10 9:32
 * @Description TODO
 * @Version 1.0
 **/
@Slf4j
@Service
public class TypeServiceImpl implements TypeService {

    private final ItemMapper itemMapper;
    private final CategoryMapper categoryMapper;

    public TypeServiceImpl(ItemMapper itemMapper, CategoryMapper categoryMapper) {
        this.itemMapper = itemMapper;
        this.categoryMapper = categoryMapper;
    }

    @Override
    public PageVO listType(Integer pageNum, Integer pageSize, Integer categoryId, String name) {
        IPage<TypeResult> page = new Page<TypeResult>().setCurrent(pageNum).setSize(pageSize);
        IPage<TypeResult> typeResultIPage = itemMapper.selectMyPage(page, categoryId, name);
        return PageVO.convert(typeResultIPage);
    }

    @Override
    public PageVO listCategory(Integer pageNum, Integer pageSize) {
        IPage<Category> page = new Page<Category>().setCurrent(pageNum).setSize(pageSize);
        IPage<Category> typeResultIPage = categoryMapper.selectPage(page, null);
        return PageVO.convert(typeResultIPage);
    }

    @Override
    public void addCategory(String name) {
        categoryMapper.insert(new Category().setName(name));
    }

    @Override
    public void addItem(ItemParam param) {
        itemMapper.insert(new Item()
                .setCategoryId(param.getCategoryId())
                .setName(param.getName())
                .setPrice(param.getPrice()));
    }

    @Override
    public List<Category> allCategory() {
        return categoryMapper.selectList(null);
    }


}
